package com.hdoit.ares.web.advertisement.controllers;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.hdoit.ares.logical.advertisement.model.AdvertisementLocation;
import com.hdoit.ares.logical.advertisement.service.IAdvertisementLocationService;
import com.hdoit.ares.logical.viewmodel.advertisement.AdvertisementLocationSearch;
import com.hdoit.ares.selfcommon.enums.advertisement.OpenState;
import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;

/**
 * @author      yujinhui
 * @email       
 * @createtime  2018年03月13日 19:18:32
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/advertisement/advertisementLocation/")
public class AdvertisementLocationController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/advertisement/advertisementLocation/";
    }

    private IAdvertisementLocationService advertisementLocationService;
    public IAdvertisementLocationService getAdvertisementLocationService() {
        return advertisementLocationService;
    }
    @Resource
    public void setAdvertisementLocationService(IAdvertisementLocationService advertisementLocationService) {
        this.advertisementLocationService = advertisementLocationService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="advertisementLocationDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,AdvertisementLocationSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(advertisementLocationService.queryCount(search));
        List<AdvertisementLocation> advertisementLocations= advertisementLocationService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("advertisementLocations", advertisementLocations);
        model.addAttribute("advertisementLocationSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String advertisementLocationDetailed(Model model,String key){
        AdvertisementLocation advertisementLocation=new AdvertisementLocation();
        advertisementLocation.setIsOpen(OpenState.Open);
        if(!StringUtils.isEmpty(key)){
            advertisementLocation=advertisementLocationService.queryModel(key);
        }
        model.addAttribute("advertisementLocation", advertisementLocation);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,AdvertisementLocation advertisementLocation){
        if(StringUtils.isEmpty(advertisementLocation.getId())){
            advertisementLocation.setId(CommonUtils.getKeyId());
            advertisementLocation.setCreateTime(DateUtils.getCurrent());
            advertisementLocation.setCreateUserId(getLoginModel().getLoginId());
            advertisementLocation.setCreateUserName(getLoginModel().getLoginName());
            advertisementLocation.setModelStatus(ModelStatus.add);
        }else{
            advertisementLocation.setModifyDate(DateUtils.getCurrent());
            advertisementLocation.setModifyUserId(getLoginModel().getLoginId());
            advertisementLocation.setModifyUserName(getLoginModel().getLoginName());
            advertisementLocation.setModelStatus(ModelStatus.update);
        }

        ActionResult actionResult;
        if(advertisementLocationService.applyChange(advertisementLocation)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("advertisementLocation", advertisementLocation);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    /*@RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(advertisementLocationService.deleteFakeByKey(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }*/
}